define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'community/index',
                    add_url: 'community/add',
                    edit_url: 'community/edit',
                    del_url: 'community/del',
                    multi_url: 'community/multi',
                    table: 'community',
                }
            },
            {
                custom:{'审核通过':'success','审核未通过':'danger','审核中':'warning'}
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                showToggle: false,
                showExport: false,
                searchFormVisible: true,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id'), operate:false},
                        {field: 'mobile', title: __('Mobile'),operate: 'LIKE %...%'},
                        {field: 'name', title: __('Name'),operate: 'LIKE %...%'},
                        {field: 'person', title: __('Person'),operate: 'LIKE %...%'},
                        {field: 'scale', title: __('Scale'), formatter: Table.api.formatter.scale, searchList: {'1': __('Scale 1'), '2': __('Scale 2'), '3': __('Scale 3'), '4': __('Scale 4'),'5': __('Scale 5'),'6': __('Scale 6') }, style: 'min-width:100px;'},
                        {field: 'avatar', title: __('Avatar'), operate:false,formatter: Controller.api.formatter.image},
                        {field: 'integration', title: __('Integration'), operate:false},
                        {field: 'setup_time', title: __('Setup_time'), formatter: Table.api.formatter.datetime, operate: 'BETWEEN', type: 'datetime', addclass: 'datetimepicker', data: 'data-date-format="YYYY-MM-DD"'},
                        {field: 'setup_addr', title: __('Setup_addr'),operate: 'LIKE %...%'},
                        {field: 'is_pay', title: __('Is_pay'), align: 'center', formatter: Controller.api.formatter.isPay, searchList: {'0': '不需要', '1': '需要'}, style: 'min-width:100px;'},
                        {field: 'year_fee', title: __('Year_fee'), operate:false},
                        {field: 'sum_income', title: __('Sum_income'), operate:false},
                        {field: 'wallet_money', title: __('Wallet_money'), operate:false},
                        {field: 'website',title:"社群网站",operate: 'LIKE %...%'},
                        {field: 'wechat',title:"微信公众号",operate: 'LIKE %...%'},
                        {field: 'is_company_text', title: __('Is_company'), operate:false},
                        {field: 'heldsdata_text', title: __('Heldsdata'), operate:false, formatter: Table.api.formatter.label,custom: 'warning',},
                        {field: 'state', title: __('State'), formatter: Table.api.formatter.flag, searchList: {'0': __('State 0'), '1': __('State 1'), '2': __('State 2')}, style: 'min-width:100px;'},
                        {field: 'operate', title: __('Operate'), table: table, 
                            buttons:[{
                                name: 'cmlist', 
                                text: '会员列表', 
                                classname: 'btn btn-xs btn-info btn-fields btn-addtabs', 
                                icon: 'fa fa-list', 
                                url: 'communityMember/index?community_id={ids}'
                            }],
                            events: Table.api.events.operate, formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            formatter: {
                isPay: function (value, row, index) {
                    return "<a href='javascript:;' class='btn btn-" + (value ? "info" : "default") + " btn-xs btn-change' data-id='"
                            + row.id + "' data-params='is_pay=" + (value ? 0 : 1) + "'>" + (value ? '需要' : '不需要') + "</a>";
                },
                image: function (value, row, index) {
                        return '<a href="' + row.avatar + '" target="_blank"><img src="' +  value  + '" alt="" style="max-height:50px;max-width:60px"></a>';
                },
            },
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});